package com.yangzb;

import cn.hutool.core.io.resource.ClassPathResource;
import cn.hutool.core.util.StrUtil;
import com.google.common.base.Stopwatch;
import lombok.extern.slf4j.Slf4j;
import org.apache.tools.ant.util.ClasspathUtils;

import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

/**
 * @author: yangzb
 * @date 2022/12/30 4:26 PM
 **/
@Slf4j
public class HdfsDownloader implements Callable<TaskInfo> {
    protected HdfsConfig hdfsConfig;
    protected Consumer<TaskInfo> consumer;
    protected TaskInfo taskInfo;
    public HdfsDownloader(HdfsConfig hdfsConfig, TaskInfo taskInfo, Consumer<TaskInfo> consumer){
        this.hdfsConfig = hdfsConfig;
        this.taskInfo = taskInfo;
        this.consumer = consumer;
    }
    @Override
    public TaskInfo call() {
        Stopwatch stopwatch  = Stopwatch.createStarted();
        try {
            HdfsUtil.download(taskInfo.getHdfsFile(), taskInfo.getLocalFile());
            if(null != consumer){
                consumer.accept(taskInfo);
            }
        } catch (Exception e) {
            log.error("",e);
            taskInfo.setException(e);
            taskInfo.setFailedStep(TaskInfo.FailedStep.DOWNLOAD);
        }finally {
            stopwatch.stop();
            log.info("下载:{},耗时：{}",taskInfo.getHdfsFile(),stopwatch);
        }
        return taskInfo;
    }
}
